enum ZombieState {
	Run, Dead
}

class Zombie extends egret.Sprite {

	private _body: egret.Bitmap;
	private _speed: number = 200;
	protected _frames_Run: egret.Texture[] = [];
	private _frames_RunIndex: number = 0;
	private _frameTimer: number = 0;
	private _frameCd: number = 100;

	private _frames_Die: egret.Texture[] = [];
	private _frames_DieIndex: number = 0;
	public _gameUI: GameUI;
	public zombieState: ZombieState = ZombieState.Run;

	public HP = 1;
	public killScore = 10;

	public constructor() {
		super();
		this._body = new egret.Bitmap(RES.getRes("enemy_05_walk_front_00_png"));
		this.addChild(this._body);
		this.initZombie();

		for (var i = 0; i < 6; i++) {
			this._frames_Die.push(RES.getRes("enemy_dead_01_0" + i + "_png"));
		}
	}

	protected initZombie() {
		for (var i = 0; i < 7; i++) {
			this._frames_Run.push(RES.getRes("enemy_05_walk_front_0" + i + "_png"));
		}
	}

	public onUpdate(span: number) {
		if (this.zombieState == ZombieState.Run) {
			this.y += this._speed * (span / 1000);
		}
		if (this.y >= 900) {
			this.Destory();
			if (this._gameUI != null) {
				this._gameUI.Time -= 1000;
			}
		}

		this._frameTimer += span;
		if (this._frameTimer > this._frameCd) {
			this._frameTimer = 0;
			if (this.zombieState == ZombieState.Run) {
				if (this._frames_RunIndex >= this._frames_Run.length) {
					this._frames_RunIndex = 0;
				}
				this._body.texture = this._frames_Run[this._frames_RunIndex];
				this._frames_RunIndex += 1;
			} else if (this.zombieState == ZombieState.Dead) {
				if (this._frames_DieIndex < this._frames_Die.length) {
					this._body.texture = this._frames_Die[this._frames_DieIndex];
					this._frames_DieIndex += 1;
				}
				this.alpha -= 0.02;
				if (this.alpha <= 0) {
					this.Destory();
				}
			}
		}

	}

	public Dead() {
		this.zombieState = ZombieState.Dead;
		this._gameUI.addScoreEffect(this.x, this.y, this.killScore);
	}

	public Hit(damage: number) {
		this.HP -= damage;
		this._gameUI.addAttackEffect(this.x, this.y);
		if (this.HP <= 0) {
			this.Dead();
			return;
		}
	}

	public Destory() {
		this.parent.removeChild(this);
	}

	public getColBlock(): egret.Rectangle {
		return new egret.Rectangle(this.x, this.y, 56, 66);
	}
}

class Zombie1 extends Zombie {
	protected initZombie() {
		this.HP = 3;
		this.killScore = 30;

		for (var i = 0; i < 7; i++) {
			this._frames_Run.push(RES.getRes("enemy_08_walk_front_0" + i + "_png"));
		}
	}
}

